package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.anotation.NeedLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.management.SubscribableChannelManagement;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;
	private SubscribableChannelManagement subscribableChannelManagement;


	//Request URL: http://localhost:8500/api/album/trackInfo/uploadTrack
	@PostMapping("/uploadTrack")
	@Operation(summary = "上传音频文件--腾讯VOD")
	public Result uploadTrack(@RequestPart MultipartFile file){
		Map<String, Object> map = trackInfoService.uploadTrack(file);
		return Result.ok(map);
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/saveTrackInfo
	@PostMapping("/saveTrackInfo")
	@Operation(summary = "保存声音信息")
	@NeedLogin
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/findUserTrackPage/1/10
	@PostMapping("/findUserTrackPage/{pageNum}/{pageSize}")
	@Operation(summary = "分页查询用户声音信息")
	@NeedLogin
	public Result findUserTrackPage(@PathVariable Long pageNum,
									@PathVariable Long pageSize,
									@RequestBody TrackInfoQuery query){
		IPage<TrackListVo> trackInfoIPage = new Page<>(pageNum, pageSize);
		query.setUserId(AuthContextHolder.getUserId());
		trackInfoIPage = trackInfoService.findUserTrackPage(trackInfoIPage,query);
		return Result.ok(trackInfoIPage);
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/getTrackInfo/51942
	@GetMapping("/getTrackInfo/{trackId}")
	@Operation(summary = "根据id查询音频信息")
	public Result getTrackInfo(@PathVariable Long trackId){
		TrackInfo trackInfoVo = trackInfoService.getTrackInfo(trackId);
		return Result.ok(trackInfoVo);
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/updateTrackInfo/51942
	@PutMapping("/updateTrackInfo/{trackId}")
	@Operation(summary = "修改音频信息")
	public Result updateTrackInfo(@PathVariable Long trackId,
								  @RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/removeTrackInfo/51944
	@DeleteMapping("/removeTrackInfo/{trackId}")
	@Operation(summary = "删除音频信息")
	public Result removeTrackInfo(@PathVariable Long trackId){
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/findAlbumTrackPage/946/1/10
	@GetMapping("/findAlbumTrackPage/{albumId}/{pageNum}/{pageSize}")
	@Operation(summary = "分页查询详情专辑下的音频信息")
	@NeedLogin(required = false)
	public Result findAlbumTrackPage(@PathVariable Long albumId,
									 @PathVariable Long pageNum,
									 @PathVariable Long pageSize){
		IPage<AlbumTrackListVo> page = new Page(pageNum,pageSize);
		page = trackInfoService.findAlbumTrackPage(albumId,page);
		return Result.ok(page);
	}


	//Request URL: http://localhost:8500/api/album/trackInfo/getTrackStatVo/51946
	/**
	 * 根据音频id获取音频的统计信息
	 * @param trackId 音频id
	 * @return 音频统计信息
	 */
	@GetMapping("/getTrackStatVo/{trackId}")
	@Operation(summary = "根据id查询音频统计信息")
	public Result getTrackStatVo(@PathVariable Long trackId){
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}


	//Request URL: http://localhost:8500/api/album/trackInfo/findUserTrackPaidList/51054
	/**
	 * 根据音频id返回当前用户访问购买这个音频的展示数据
	 * @param trackId 音频id
	 * @return 用户购买页面
	 */
	@GetMapping("/findUserTrackPaidList/{trackId}")
	@Operation(summary = "根据音频id获取当前用户针对该音频的购买详情页数据")
	@NeedLogin
	public Result findUserTrackPaidList(@PathVariable Long trackId){
		Long userId = AuthContextHolder.getUserId();
		List<Map<String,Object>> result = trackInfoService.findUserTrackPaidList(trackId,userId);
		return Result.ok(result);
	}

}

